package com.hrp.book.ui.bookshelf

import android.app.Application
import com.blankj.utilcode.util.LogUtils
import com.hrp.analyzer.core.helper.DebugHelper
import com.hrp.analyzer.core.helper.RuleHelper
import com.hrp.analyzer.core.helper.RuleTester
import com.hrp.analyzer.core.model.AnalyzerRule
import com.hrp.analyzer.core.model.ContentRule
import com.hrp.analyzer.core.rule.CommonRule
import com.hrp.analyzer.spider.NovelSpider
import com.hrp.analyzer.spider.SearchEngines
import com.hrp.book.base.BaseViewModel

class BookShelfModel(application: Application) : BaseViewModel(application) {
    override fun initialize() {
//        viewModelScope.launch(Dispatchers.IO) {
//            DebugHelper.subscribe {
//                LogUtils.getConfig().stackOffset = 7
//                LogUtils.e("test_", it)
//                LogUtils.getConfig().stackOffset = 0
//            }
//            delay(1000)
//            // testSearch()
//            baiduSearch()
//        }
    }


    fun test() {
        LogUtils.e("test_", "test_")
        DebugHelper.subscribe {
            LogUtils.getConfig().stackOffset = 7
            LogUtils.e("test_", it)
            LogUtils.getConfig().stackOffset = 0
        }
        try {
            val url = "https://www.xbiquge.la/13/13959/5939034.html"
            val spider = NovelSpider()
            // 正文规则封装
            // 正文规则封装
            val analyzerRule = AnalyzerRule()
            analyzerRule.audio = true
            analyzerRule.site = url
            val contentRule = ContentRule()
            val rule: CommonRule = CommonRule.create("//div[@id=\"content\"]/text()")
            rule.script = "var t=utils.match(source, \"//a[contains(text(),'查看完整目录')]/@href\")\n" +
                    "url=utils.absUrl(url,t)\n" +
                    "source=utils.get(url)\n" +
                    "result=url+source\n"
            contentRule.content = rule
            analyzerRule.content = contentRule
            spider.rule = analyzerRule
            spider.content(url) {
                LogUtils.e("test_", it)
            }
        } catch (e: Exception) {
            LogUtils.e("test_", "test_", e)
        }
    }

    fun baiduSearch() {
        SearchEngines().searchBaidu("完美世界")?.forEach {
            LogUtils.e("test_", it)
        }
    }

    fun testSearch() {
        RuleHelper.loadRules(
            "[{\"name\":\"追书\",\"site\":\"https://shuapi.jiaston.com/\",\"enabled\":false,\"audio\":false,\"weight\":0,\"params\":{},\"content\":{\"params\":{\"charset\":\"UTF-8\",\"mediaType\":\"application/json\",\"dynamicDelayTime\":500},\"content\":{\"type\":\"\$.\",\"rule\":\"\$.data.content\",\"replace\":[{\"from\":\"\\\\f\\\\t\\\\n\",\"to\":\"\\f\\t\\n\"},{\"from\":\"\\\\r\\\\n\",\"to\":\"\\r\\n\"}]},\"next\":{}},\"toc\":{\"params\":{\"charset\":\"UTF-8\",\"mediaType\":\"application/json\",\"dynamicDelayTime\":500},\"list\":{\"type\":\"\$.\",\"rule\":\"\$.data.list[*].list.*\"},\"name\":{\"type\":\"\$.\",\"rule\":\"\$.name\"},\"url\":{\"type\":\"\$.\",\"rule\":\"\$.id\",\"script\":\"result = result+\\\".html\\\"\"},\"next\":{\"type\":\"xpath\",\"rule\":\"//a[text()~='.*?下[一]{0,1}[页节].*']/@href\"},\"enableNext\":true,\"filter\":true,\"reverse\":false,\"autoNext\":true,\"forceNext\":false},\"detail\":{\"title\":{},\"author\":{},\"broadcast\":{},\"category\":{},\"wordCount\":{},\"introduce\":{},\"latestChapterName\":{},\"latestChapterUrl\":{},\"coverUrl\":{},\"state\":{},\"updateTime\":{}},\"search\":{\"params\":{\"url\":\"https://sou.jiaston.com/search.aspx?key={{keyword}}&page={{page}}&siteid=app2\",\"dynamicDelayTime\":500},\"list\":{\"type\":\"\$.\",\"rule\":\"\$.data\"},\"detailPage\":{},\"detail\":{\"url\":{\"type\":\"\$.\",\"rule\":\"\$.Id\",\"script\":\"\\\"https://shuapi.jiaston.com/book/\\\"+result+\\\"/\\\"\"},\"title\":{\"type\":\"\$.\",\"rule\":\"\$.Name\"},\"author\":{\"type\":\"\$.\",\"rule\":\"\$.Author\"},\"broadcast\":{},\"category\":{\"type\":\"\$.\",\"rule\":\"\$.CName\"},\"wordCount\":{},\"introduce\":{\"type\":\"\$.\",\"rule\":\"\$.Desc\"},\"latestChapterName\":{\"type\":\"\$.\",\"rule\":\"\$.LastChapter\"},\"latestChapterUrl\":{},\"coverUrl\":{},\"state\":{\"type\":\"\$.\",\"rule\":\"\$.BookStatus\"},\"updateTime\":{\"type\":\"\$.\",\"rule\":\"\$.UpdateTime\"}}}},{\"name\":\"笔趣阁\",\"site\":\"https://www.xbiquge.la/\",\"enabled\":true,\"audio\":false,\"weight\":0,\"params\":{},\"content\":{\"content\":{\"type\":\"auto\",\"rule\":\"1\"},\"next\":{\"type\":\"xpath\",\"rule\":\"//a[text()~='.*?下[一]{0,1}[页节].*']/@href\"}},\"toc\":{\"list\":{},\"name\":{},\"url\":{},\"next\":{\"type\":\"xpath\",\"rule\":\"//a[text()~='.*?下[一]{0,1}[页节].*']/@href\"},\"enableNext\":true,\"filter\":true,\"reverse\":false,\"autoNext\":true,\"forceNext\":false},\"detail\":{\"title\":{},\"author\":{},\"broadcast\":{},\"category\":{},\"wordCount\":{},\"introduce\":{},\"latestChapterName\":{},\"latestChapterUrl\":{},\"coverUrl\":{},\"state\":{},\"updateTime\":{}},\"search\":{\"params\":{\"url\":\"https://www.xbiquge.la/modules/article/waps.php?searchkey={{keyword}}\",\"method\":\"GET\",\"charset\":\"UTF-8\",\"mediaType\":\"application/x-www-form-urlencoded\",\"body\":\"\",\"dynamicDelayTime\":500},\"list\":{\"type\":\"xpath\",\"rule\":\"//table//tr[position()>1]\"},\"detailPage\":{},\"detail\":{\"url\":{\"page\":\"search\",\"type\":\"xpath\",\"rule\":\"//td[@class='even'][1]//a//@href\"},\"title\":{\"type\":\"xpath:\",\"rule\":\"//td[@class='even'][1]//a//text()\"},\"author\":{\"type\":\"xpath:\",\"rule\":\"//td[@class='even'][2]//text()\"},\"broadcast\":{},\"category\":{},\"wordCount\":{},\"introduce\":{},\"latestChapterName\":{\"type\":\"xpath:\",\"rule\":\"//td[@class='odd'][1]//a//text()\"},\"latestChapterUrl\":{\"type\":\"xpath:\",\"rule\":\"//td[@class='odd'][1]//a//@href\"},\"coverUrl\":{},\"state\":{},\"updateTime\":{\"type\":\"xpath:\",\"rule\":\"//td[@class='odd'][2]//text()\"}}}},{\"name\":\"着笔中文网\",\"site\":\"https://www.zbzw.la/\",\"enabled\":true,\"audio\":false,\"weight\":2,\"params\":{},\"content\":{\"content\":{\"type\":\"xpath\",\"rule\":\"//div[@class=\\\"showtxt\\\"]/text()\",\"replace\":[{\"from\":\"regex:\\\\(http://www.zbzw.la/book/[\\\\s\\\\S]*m.zbzw.la\",\"to\":\"\"},{\"from\":\"<br />\",\"to\":\"\\n\"}]},\"next\":{\"type\":\"xpath\",\"rule\":\"//a[text()~='.*?下[一]{0,1}[页节].*']/@href\"}},\"toc\":{\"list\":{},\"name\":{},\"url\":{},\"next\":{\"type\":\"xpath\",\"rule\":\"//a[text()~='.*?下[一]{0,1}[页节].*']/@href\"},\"enableNext\":true,\"filter\":true,\"reverse\":false,\"autoNext\":true,\"forceNext\":false},\"detail\":{\"title\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:book_name\\\"]//@content\"},\"author\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:author\\\"]//@content\"},\"broadcast\":{},\"category\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:category\\\"]//@content\"},\"wordCount\":{\"type\":\"xpath:\",\"rule\":\"//div[@class='small']/span[4]\",\"replace\":[{\"from\":\"字数：\",\"to\":\"\"}]},\"introduce\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:description\\\"]//@content\"},\"latestChapterName\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:latest_chapter_name\\\"]//@content\"},\"latestChapterUrl\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:latest_chapter_url\\\"]//@content\"},\"coverUrl\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:image\\\"]//@content\"},\"state\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:status\\\"]//@content\"},\"updateTime\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:update_time\\\"]//@content\"}},\"search\":{\"params\":{\"url\":\"https://www.zbzw.la/search/?ie=gbk&q={{keyword}}\",\"dynamicDelayTime\":500},\"list\":{\"type\":\"xpath\",\"rule\":\"//div[@class=\\\"l bd\\\"]//li\"},\"detailPage\":{\"type\":\"xpath\",\"rule\":\"//span[2]//a//@href\"},\"detail\":{\"url\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:read_url\\\"]//@content\"},\"title\":{\"type\":\"xpath\",\"rule\":\"//meta[@property=\\\"og:novel:book_name\\\"]//@content\"},\"author\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:author\\\"]//@content\"},\"broadcast\":{},\"category\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:category\\\"]//@content\"},\"wordCount\":{\"type\":\"xpath\",\"rule\":\"//div[@class='small']/span[4]\",\"replace\":[{\"from\":\"字数：\",\"to\":\"\"}]},\"introduce\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:description\\\"]//@content\"},\"latestChapterName\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:latest_chapter_name\\\"]//@content\"},\"latestChapterUrl\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:latest_chapter_url\\\"]//@content\"},\"coverUrl\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:image\\\"]//@content\"},\"state\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:status\\\"]//@content\"},\"updateTime\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:update_time\\\"]//@content\"}}}}]"
        )
        val tester = RuleTester(RuleHelper.rules()[1]) {
            LogUtils.e("test_", it)
        }
        tester.test("完美")
//        RuleHelper.loadRules(
//            "[{\"name\":\"追书\",\"site\":\"https://shuapi.jiaston.com/\",\"enabled\":false,\"audio\":false,\"weight\":0,\"params\":{},\"content\":{\"params\":{\"charset\":\"UTF-8\",\"mediaType\":\"application/json\",\"dynamicDelayTime\":500},\"content\":{\"type\":\"\$.\",\"rule\":\"\$.data.content\",\"replace\":[{\"from\":\"\\\\f\\\\t\\\\n\",\"to\":\"\\f\\t\\n\"},{\"from\":\"\\\\r\\\\n\",\"to\":\"\\r\\n\"}]},\"next\":{}},\"toc\":{\"params\":{\"charset\":\"UTF-8\",\"mediaType\":\"application/json\",\"dynamicDelayTime\":500},\"list\":{\"type\":\"\$.\",\"rule\":\"\$.data.list[*].list.*\"},\"name\":{\"type\":\"\$.\",\"rule\":\"\$.name\"},\"url\":{\"type\":\"\$.\",\"rule\":\"\$.id\",\"script\":\"result = result+\\\".html\\\"\"},\"next\":{\"type\":\"xpath\",\"rule\":\"//a[text()~='.*?下[一]{0,1}[页节].*']/@href\"},\"enableNext\":true,\"filter\":true,\"reverse\":false,\"autoNext\":true,\"forceNext\":false},\"detail\":{\"title\":{},\"author\":{},\"broadcast\":{},\"category\":{},\"wordCount\":{},\"introduce\":{},\"latestChapterName\":{},\"latestChapterUrl\":{},\"coverUrl\":{},\"state\":{},\"updateTime\":{}},\"search\":{\"params\":{\"url\":\"https://sou.jiaston.com/search.aspx?key={{keyword}}&page={{page}}&siteid=app2\",\"dynamicDelayTime\":500},\"list\":{\"type\":\"\$.\",\"rule\":\"\$.data\"},\"detailPage\":{},\"detail\":{\"url\":{\"type\":\"\$.\",\"rule\":\"\$.Id\",\"script\":\"\\\"https://shuapi.jiaston.com/book/\\\"+result+\\\"/\\\"\"},\"title\":{\"type\":\"\$.\",\"rule\":\"\$.Name\"},\"author\":{\"type\":\"\$.\",\"rule\":\"\$.Author\"},\"broadcast\":{},\"category\":{\"type\":\"\$.\",\"rule\":\"\$.CName\"},\"wordCount\":{},\"introduce\":{\"type\":\"\$.\",\"rule\":\"\$.Desc\"},\"latestChapterName\":{\"type\":\"\$.\",\"rule\":\"\$.LastChapter\"},\"latestChapterUrl\":{},\"coverUrl\":{},\"state\":{\"type\":\"\$.\",\"rule\":\"\$.BookStatus\"},\"updateTime\":{\"type\":\"\$.\",\"rule\":\"\$.UpdateTime\"}}}},{\"name\":\"笔趣阁\",\"site\":\"https://www.xbiquge.la/\",\"enabled\":true,\"audio\":false,\"weight\":0,\"params\":{},\"content\":{\"content\":{\"type\":\"auto\",\"rule\":\"1\"},\"next\":{\"type\":\"xpath\",\"rule\":\"//a[text()~='.*?下[一]{0,1}[页节].*']/@href\"}},\"toc\":{\"list\":{},\"name\":{},\"url\":{},\"next\":{\"type\":\"xpath\",\"rule\":\"//a[text()~='.*?下[一]{0,1}[页节].*']/@href\"},\"enableNext\":true,\"filter\":true,\"reverse\":false,\"autoNext\":true,\"forceNext\":false},\"detail\":{\"title\":{},\"author\":{},\"broadcast\":{},\"category\":{},\"wordCount\":{},\"introduce\":{},\"latestChapterName\":{},\"latestChapterUrl\":{},\"coverUrl\":{},\"state\":{},\"updateTime\":{}},\"search\":{\"params\":{\"url\":\"https://www.xbiquge.la/modules/article/waps.php?searchkey={{keyword}}\",\"method\":\"GET\",\"charset\":\"UTF-8\",\"mediaType\":\"application/x-www-form-urlencoded\",\"body\":\"\",\"dynamicDelayTime\":500},\"list\":{\"type\":\"xpath\",\"rule\":\"//table//tr[position()>1]\"},\"detailPage\":{},\"detail\":{\"url\":{\"page\":\"search\",\"type\":\"xpath\",\"rule\":\"//td[@class='even'][1]//a//@href\"},\"title\":{\"type\":\"xpath:\",\"rule\":\"//td[@class='even'][1]//a//text()\"},\"author\":{\"type\":\"xpath:\",\"rule\":\"//td[@class='even'][2]//text()\"},\"broadcast\":{},\"category\":{},\"wordCount\":{},\"introduce\":{},\"latestChapterName\":{\"type\":\"xpath:\",\"rule\":\"//td[@class='odd'][1]//a//text()\"},\"latestChapterUrl\":{\"type\":\"xpath:\",\"rule\":\"//td[@class='odd'][1]//a//@href\"},\"coverUrl\":{},\"state\":{},\"updateTime\":{\"type\":\"xpath:\",\"rule\":\"//td[@class='odd'][2]//text()\"}}}},{\"name\":\"着笔中文网\",\"site\":\"https://www.zbzw.la/\",\"enabled\":true,\"audio\":false,\"weight\":2,\"params\":{},\"content\":{\"content\":{\"type\":\"xpath\",\"rule\":\"//div[@class=\\\"showtxt\\\"]/text()\",\"replace\":[{\"from\":\"regex:\\\\(http://www.zbzw.la/book/[\\\\s\\\\S]*m.zbzw.la\",\"to\":\"\"},{\"from\":\"<br />\",\"to\":\"\\n\"}]},\"next\":{\"type\":\"xpath\",\"rule\":\"//a[text()~='.*?下[一]{0,1}[页节].*']/@href\"}},\"toc\":{\"list\":{},\"name\":{},\"url\":{},\"next\":{\"type\":\"xpath\",\"rule\":\"//a[text()~='.*?下[一]{0,1}[页节].*']/@href\"},\"enableNext\":true,\"filter\":true,\"reverse\":false,\"autoNext\":true,\"forceNext\":false},\"detail\":{\"title\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:book_name\\\"]//@content\"},\"author\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:author\\\"]//@content\"},\"broadcast\":{},\"category\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:category\\\"]//@content\"},\"wordCount\":{\"type\":\"xpath:\",\"rule\":\"//div[@class='small']/span[4]\",\"replace\":[{\"from\":\"字数：\",\"to\":\"\"}]},\"introduce\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:description\\\"]//@content\"},\"latestChapterName\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:latest_chapter_name\\\"]//@content\"},\"latestChapterUrl\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:latest_chapter_url\\\"]//@content\"},\"coverUrl\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:image\\\"]//@content\"},\"state\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:status\\\"]//@content\"},\"updateTime\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:update_time\\\"]//@content\"}},\"search\":{\"params\":{\"url\":\"https://www.zbzw.la/search/?ie=gbk&q={{keyword}}\",\"dynamicDelayTime\":500},\"list\":{\"type\":\"xpath\",\"rule\":\"//div[@class=\\\"l bd\\\"]//li\"},\"detailPage\":{\"type\":\"xpath\",\"rule\":\"//span[2]//a//@href\"},\"detail\":{\"url\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:read_url\\\"]//@content\"},\"title\":{\"type\":\"xpath\",\"rule\":\"//meta[@property=\\\"og:novel:book_name\\\"]//@content\"},\"author\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:author\\\"]//@content\"},\"broadcast\":{},\"category\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:category\\\"]//@content\"},\"wordCount\":{\"type\":\"xpath\",\"rule\":\"//div[@class='small']/span[4]\",\"replace\":[{\"from\":\"字数：\",\"to\":\"\"}]},\"introduce\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:description\\\"]//@content\"},\"latestChapterName\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:latest_chapter_name\\\"]//@content\"},\"latestChapterUrl\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:latest_chapter_url\\\"]//@content\"},\"coverUrl\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:image\\\"]//@content\"},\"state\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:status\\\"]//@content\"},\"updateTime\":{\"type\":\"xpath:\",\"rule\":\"//meta[@property=\\\"og:novel:update_time\\\"]//@content\"}}}}]"
//        )
//        val searchSpider = SearchSpider(RuleHelper.rules())
//        searchSpider.setOnNewItemAddHandler { e ->
//            LogUtils.e("test_", e.getTitle().toString() + "  -  " + e.getUrl())
//        }
//        searchSpider.search("完美")
    }
}